package foundation.matrix_multiplication;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // 阶数
        int m = sc.nextInt(); // 幂
        int[][] nums = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                nums[i][j] = sc.nextInt();
            }
        }
        if (m > 0) {
            int[][] res = nums;
            while (m > 1) {
                res = count(res, nums);
                m --;
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    System.out.print(res[i][j] + " ");
                }
                System.out.println();
            }
        } else {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (i != j) {
                        System.out.print(0 + " ");
                    } else {
                        System.out.print(1 + " ");
                    }
                }
                System.out.println();
            }
        }
    }

    /**
     * 计算矩阵nums和tmp的成绩
     * @param nums
     * @param tmp
     * @return
     */
    public static int[][] count(int[][] nums, int[][] tmp) {
        int len = tmp.length;
        int res[][] = new int[len][len];
        int j = 0;
        for (int i = 0; i < len; i++) {
            int[] n = nums[i];
            for (int k = 0; k < len; k++) {
                int t = 0;
                for (int m = 0; m < len; m++) {
                    t += n[m] * tmp[m][k];
                }
                res[i][j] = t;
                j = (j + 1) % len;
            }
        }
        return res;
    }
}
